﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Input;
using VMDS.Business.Entities;
using VMDS.GUIClient.Helper;
using VMDS.GUIClient.Model;

namespace VMDS.GUIClient.ViewModels.Management.Storages
{
    internal class StorageManagementViewModel : BaseViewModel
    {
        private bool _browserVisible;
        private Storage _cstore;

        public StorageManagementViewModel()
        {
            ManagerDataStore.Instance.PropertyChanged += Instance_PropertyChanged;
            BrowserVisible = false;
            AddVisible = false;
            
            DelStorage = delcommand();
        }

        public ICommand ShowEdit { get { return new BaseCommand(() => { EditVisible = !EditVisible; },canshow); } }
        bool canshow()
        {
            return CurrentStorage != null;
        }
        private string _vmess;
        public string ValidationMessage
        {
            get { return _vmess; }
            set
            {
                _vmess = value;
                OnPropertyChanged(() => ValidationMessage);
            }
        }

        private BaseCommand _edit;

        public ICommand Edit
        {
            get { return _edit; }
            private set
            {
                _edit = (BaseCommand)value;
                OnPropertyChanged(() => Edit);
            }
        }

        private bool _evis;
        public bool EditVisible
        {
            get { return _evis; }
            set
            {
                _evis = value;
                OnPropertyChanged(() => EditVisible);
            }
        }

        private bool _loadVis;
        public bool LoadVisible
        {
            get { return _loadVis; }
            set
            {
                _loadVis = value;
                OnPropertyChanged(() => LoadVisible);
            }
        }

        BaseCommand command()
        {
            return new BaseCommand(edit, canEdit);
        }

        void edit()
        {
            try
            {
                MsLocator.Client.UpdateStorage(new Storage() {Id=CurrentStorage.Id, Name = StName, Path = StPath });
                ManagerDataStore.Instance.LoadData();
                EditVisible = false;
            }
            catch (Exception)
            {
                throw;
            }
        }


        private string _name;
        public string StName
        {
            get { return _name; }
            set
            {
                _name = value; OnPropertyChanged(() => StName);
                Edit = command();
            }
        }

        private string _path;
        public string StPath
        {
            get { return _path; }
            set
            {
                _path = value; OnPropertyChanged(() => StPath);
                Edit = command();
            }
        }

        private bool canEdit()
        {
            bool c1, c2, c3 = false;
            c1 = !String.IsNullOrWhiteSpace(StName);
            c2 = !String.IsNullOrWhiteSpace(StPath);
            if (c2)
            {
                ValidationMessage = "Проверка пути...";
                c3 = Directory.Exists(StPath);
            }
            if (!c3) ValidationMessage = "Указанный путь неверен";
            if (!c2) ValidationMessage = "Введите корректный путь";
            if (!c1) ValidationMessage = "Введите корректное имя";

            bool result = c2 && c1 && c3;
            if (result) ValidationMessage = "";
            return result;
        }

        public bool BrowserVisible
        {
            get { return _browserVisible; }
            set
            {
                _browserVisible = value;
                OnPropertyChanged(() => BrowserVisible);
            }
        }

        public ObservableCollection<Storage> Storages
        {
            get { return ManagerDataStore.Instance.Storages; }
        }

        public ICommand AddStorage
        {
            get { return new BaseCommand(addstor); }
        }

        private ICommand _dc;
        public ICommand DelStorage
        {
            get { return _dc; }
            set { _dc = value;
                OnPropertyChanged(() => DelStorage);
            }
        }

        BaseCommand delcommand()
        {
            return new BaseCommand(delstor, candel);
        }

        private void load()
        {
            LoadVisible = true;
            BrowserVisible = false;
        }

        private void unload()
        {
            LoadVisible = false;
            BrowserVisible = true;
        }


        public Storage CurrentStorage
        {
            get { return _cstore; }
            set
            {
                _cstore = value;
              
                if (_cstore != null)
                {
                    StPath = _cstore.Path;
                    StName = _cstore.Name;
                }
                OnPropertyChanged(() => CurrentStorage);
                OnPropertyChanged(() => ShowEdit);
                DelStorage = delcommand();
                BrowserVisible = true;
                EditVisible = false;
            }
        }

        private bool _av;
        public bool AddVisible
        {
            get { return _av; }
            set { _av = value;
                OnPropertyChanged(() => AddVisible);
            }
        }

        private void addstor()
        {
            AddVisible = true;
            BrowserVisible = false;
        }

        private void delstor()
        {
           
            if (MessageBox.Show("Удалить " +StName+"?", "", MessageBoxButton.YesNo)== MessageBoxResult.Yes)
            {
                MsLocator.Client.DeleteStorage(CurrentStorage);
                ManagerDataStore.Instance.LoadData();
                BrowserVisible = false;
                if ((Storages != null) && (Storages.Count > 0)) CurrentStorage = Storages[0];
                else
                {
                    StName = string.Empty;
                    StPath = string.Empty;
                }
                
            }
        }
        bool candel()
        {
            return CurrentStorage != null;
        }

        private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(() => Storages);
            OnPropertyChanged(() => CurrentStorage);

        }
       
    }
}